<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>盘点编辑</title>
	<!-- import css and js --> 
<%@ include file="/common/metaInput.jsp" %>  
<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript">
var basic_Url="${ctx}/yd/logic/whcheck";
var basic_WUrl="yd/logic/whcheck";
var grid;
$(function(){
	var  column1 = [
					  { display: 'id', name: 'id', width: 100,hide:true},
					  { display: 'checkID', name: 'checkID', width: 100,hide:true},
					  { display: 'materialsID', name: 'materialsID', width: 100,hide:true},
	                  { display: '<font color="gray">物料编码</font>', name: 'code', width: 120},
	                  { display: '<font color="gray">物料名称</font>', name: 'name', width: 120},
	                  { display: '<font color="gray">物料类型</font>', name: 'maType', width: 100},
	                  { display: '<font color="gray">型号</font>', name: 'spec', width: 270},
	                  { display: '<font color="gray">单位</font>', name: 'unit', width: 80},
	                  { display: '<font color="gray">批次</font>', name: 'batch', width: 120},
	                  { display: '<font color="gray">总库存数量</font>', name: 'quantityWH', width: 80,render:function(item){
	                	  if(item.quantityWH == null ||item.quantityWH == "" || parseFloat(item.quantityWH) == 0){
	                		  return '0.0';
	                	} else {
                	 	 	return item.quantityWH;
                	 	}
	                  }},
	                  { display: '<font color="gray">总盘点数量</font>', name: 'quantityPD', width: 80,render: function (item){  
						  	item.quantityPD = item.ydengQuantityPD+item.cipinQuantityPD+item.jiupinQuantityPD+item.freezeQuantityPD; 
							if(item.quantityPD == null ||item.quantityPD == "" || parseFloat(item.quantityPD) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.quantityPD;
	                	 	}
	                  }},
					  { display: '<font color="gray">总差异数量</font>', name: 'quantityCY', width: 80,render: function (item){  
						  	item.quantityCY = item.quantityPD-item.quantityWH; 
							if(item.quantityCY == null ||item.quantityCY == "" || parseFloat(item.quantityCY) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.quantityCY;
	                	 	}
	                  }},
	                  { display: '<font color="gray">一等品库存数量</font>', name: 'ydengQuantityWH', width: 100,render:function(item){
	                	  if(item.ydengQuantityWH == null ||item.ydengQuantityWH == "" || parseFloat(item.ydengQuantityWH) == 0){
	                		  return '0.0';
	                	} else {
                	 	 	return item.ydengQuantityWH;
                	 	}
	                  }},
	                  { display: '一等品盘点数量<font color="red">*</font>', name: 'ydengQuantityPD', width: 100, editor: { type: 'float',onChanged : function(item,value){
	                		grid.updateCell('ydengQuantityCY', value-item.ydengQuantityWH, item); 
	                  }},render: function(item){
	                	  if(item.ydengQuantityPD == null ||item.ydengQuantityPD == "" || parseFloat(item.ydengQuantityPD) == 0){
	                		  return '0.0';
	                	} else {
                	 	 	return item.ydengQuantityPD;
                	 	}
	                  }},
					  { display: '<font color="gray">一等品差异数量</font>', name: 'ydengQuantityCY', width: 100,render: function (item){  
						  	item.ydengQuantityCY = item.ydengQuantityPD-item.ydengQuantityWH; 
							if(item.ydengQuantityCY == null ||item.ydengQuantityCY == "" || parseFloat(item.ydengQuantityCY) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.ydengQuantityCY;
	                	 	}
	                  }},
	                  { display: '<font color="gray">次品库存数量</font>', name: 'cipinQuantityWH', width: 100,render: function (item){  
							if(item.cipinQuantityWH == null ||item.cipinQuantityWH == "" || parseFloat(item.cipinQuantityWH) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.cipinQuantityWH;
	                	 	}
	                  }},
	                  { display: '次品盘点数量<font color="red">*</font>', name: 'cipinQuantityPD', width: 100, editor: { type: 'float',onChanged : function(item,value){
	                		grid.updateCell('cipinQuantityCY', value-item.cipinQuantityWH, item); 
	                  }},render: function(item){
	                	  if(item.cipinQuantityPD == null ||item.cipinQuantityPD == "" || parseFloat(item.cipinQuantityPD) == 0){
	                		  return '0.0';
	                	} else {
                	 	 	return item.cipinQuantityPD;
                	 	}
	                  }},
					  { display: '<font color="gray">次品差异数量</font>', name: 'cipinQuantityCY', width: 100,render: function (item){  
						  	item.cipinQuantityCY = item.cipinQuantityPD-item.cipinQuantityWH; 
							if(item.cipinQuantityCY == null ||item.cipinQuantityCY == "" || parseFloat(item.cipinQuantityCY) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.cipinQuantityCY;
	                	 	}
	                  }},
	                  { display: '<font color="gray">旧品库存数量</font>', name: 'jiupinQuantityWH', width: 100,render: function (item){  
							if(item.jiupinQuantityWH == null ||item.jiupinQuantityWH == "" || parseFloat(item.jiupinQuantityWH) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.jiupinQuantityWH;
	                	 	}
	                  }},
	                  { display: '旧品盘点数量<font color="red">*</font>', name: 'jiupinQuantityPD', width: 100, editor: { type: 'float',onChanged : function(item,value){
	                		grid.updateCell('jiupinQuantityCY', value-item.jiupinQuantityWH, item); 
	                  }},render: function (item){  
							if(item.jiupinQuantityPD == null ||item.jiupinQuantityPD == "" || parseFloat(item.jiupinQuantityPD) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.jiupinQuantityPD;
	                	 	}
	                  }},
					  { display: '<font color="gray">旧品差异数量</font>', name: 'jiupinQuantityCY', width: 100,render: function (item){  
						  	item.jiupinQuantityCY = item.jiupinQuantityPD-item.jiupinQuantityWH; 
							if(item.jiupinQuantityCY == null ||item.jiupinQuantityCY == "" || parseFloat(item.jiupinQuantityCY) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.jiupinQuantityCY;
	                	 	}
	                  }},
	                  { display: '<font color="gray">冻结库存数量</font>', name: 'freezeQuantityWH', width: 100,render: function (item){  
							if(item.freezeQuantityWH == null ||item.freezeQuantityWH == "" || parseFloat(item.freezeQuantityWH) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.freezeQuantityWH;
	                	 	}
	                  }},
	                  { display: '冻结盘点数量<font color="red">*</font>', name: 'freezeQuantityPD', width: 100, editor: { type: 'float',onChanged : function(item,value){
	                		grid.updateCell('freezeQuantityCY', value-item.freezeQuantityWH, item); 
	                  }},render: function (item){  
							if(item.freezeQuantityPD == null ||item.freezeQuantityPD == "" || parseFloat(item.freezeQuantityPD) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.freezeQuantityPD;
	                	 	}
	                  }},
					  { display: '<font color="gray">冻结差异数量</font>', name: 'freezeQuantityCY', width: 100,render: function (item){  
						  	item.freezeQuantityCY = item.freezeQuantityPD-item.freezeQuantityWH; 
							if(item.freezeQuantityCY == null ||item.freezeQuantityCY == "" || parseFloat(item.freezeQuantityCY) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.freezeQuantityCY;
	                	 	}
	                  }},
	                  { display: '<font color="gray">备注</font>', name: 'remark', width: 220,editor: { type: 'text'}}
	                  ];
	var  column2 = [
					  { display: 'id', name: 'id', width: 100,hide:true},
					  { display: 'checkID', name: 'checkID', width: 100,hide:true},
					  { display: 'materialsID', name: 'materialsID', width: 100,hide:true},
	                  { display: '<font color="gray">物料编码</font>', name: 'code', width: 120},
	                  { display: '<font color="gray">物料名称</font>', name: 'name', width: 120},
	                  { display: '<font color="gray">物料类型</font>', name: 'maType', width: 100},
	                  { display: '<font color="gray">型号</font>', name: 'spec', width: 270},
	                  { display: '<font color="gray">单位</font>', name: 'unit', width: 80},
	                  { display: '<font color="gray">批次</font>', name: 'batch', width: 120},
	                  { display: '<font color="gray">总库存数量</font>', name: 'quantityWH', width: 80},
	                  { display: '<font color="gray">总盘点数量</font>', name: 'quantityPD', width: 80},
					  { display: '<font color="gray">总差异数量</font>', name: 'quantityCY', width: 80,render: function (item){  
							if(item.quantityCY == null || parseFloat(item.quantityCY) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.quantityCY;
	                	 	}
	                  }},
	                  { display: '<font color="gray">一等品库存数量</font>', name: 'ydengQuantityWH', width: 100},
	                  { display: '一等品盘点数量<font color="red">*</font>', name: 'ydengQuantityPD', width: 100},
					  { display: '<font color="gray">一等品差异数量</font>', name: 'ydengQuantityCY', width: 100,render: function (item){  
							if(item.ydengQuantityCY == null || parseFloat(item.ydengQuantityCY) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.ydengQuantityCY;
	                	 	}
	                  }},
	                  { display: '<font color="gray">次品库存数量</font>', name: 'cipinQuantityWH', width: 100},
	                  { display: '次品盘点数量<font color="red">*</font>', name: 'cipinQuantityPD', width: 100},
					  { display: '<font color="gray">次品差异数量</font>', name: 'cipinQuantityCY', width: 100,render: function (item){  
							if(item.cipinQuantityCY == null || parseFloat(item.cipinQuantityCY) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.cipinQuantityCY;
	                	 	}
	                  }},
	                  { display: '<font color="gray">旧品库存数量</font>', name: 'jiupinQuantityWH', width: 100},
	                  { display: '旧品盘点数量<font color="red">*</font>', name: 'jiupinQuantityPD', width: 100},
					  { display: '<font color="gray">旧品差异数量</font>', name: 'jiupinQuantityCY', width: 100,render: function (item){  
							if(item.jiupinQuantityCY == null || parseFloat(item.jiupinQuantityCY) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.jiupinQuantityCY;
	                	 	}
	                  }},
	                  { display: '<font color="gray">冻结库存数量</font>', name: 'freezeQuantityWH', width: 100},
	                  { display: '冻结盘点数量<font color="red">*</font>', name: 'freezeQuantityPD', width: 100},
					  { display: '<font color="gray">冻结差异数量</font>', name: 'freezeQuantityCY', width: 100,render: function (item){  
							if(item.freezeQuantityCY == null || parseFloat(item.freezeQuantityCY) == 0){
		                		  return '0.0';
		                	} else {
	                	 	 	return item.freezeQuantityCY;
	                	 	}
	                  }},
	                  { display: '<font color="gray">备注</font>', name: 'remark', width: 220}
	                  ];
	var column3 = null;
	if ('${xx}' == 1){
		column3 = column1;
	}else {
		column3 = column2;
	}
	grid = $("#table1").ligerGrid({
        columns:  column3,
        enabledEdit: true,rownumbers:true,
        usePager: true,pageSize:1000,pageSizeOptions:[1000],
        parms:[{ name : 'id' , value : '${pd.id}'}],
      	url : basic_Url+"/griditems.do",
        width: "100%", height:'90%', checkbox: false
       /*  toolbar: {
			items : [ {
				text : '导入',
				disable : true,
				click : Importexcel,
				icon: 'add'
			}]
        } */
     });
	$("#pageloading").hide();
	
	$("#inputForm").validate({			   
		onsubmit: true,
		 /*执行ajaxsubmit  */ 
	    submitHandler: function(editform) {
	        try{			    		
	            url =  basic_Url+"/save.do";
	            var api = frameElement.api;
	        	var W = api.opener;
	            getReturnMessage(url,"inputForm",W.searchAP);
	        }catch(e){alert(e);}
	    }
	});

	if ('${flag}' == 'upload'){
		$("#tr1").hide();
		<c:forEach items='${uploaditemlist}' var='item'>
			var manager = $("#table1").ligerGetGridManager();
			manager.addRow2({
				code: "${item.code}",
				name: "${item.name}",
				maType: "${item.maType}",
				spec: "${item.spec}",
				unit: "${item.unit}",
				batch: "${item.batch}",
				quantityWH : "${item.quantityWH}",
				quantityPD : "${item.quantityPD}",
				quantityCY : "${item.quantityCY}",
				cipinQuantityWH : "${item.cipinQuantityWH}",
				cipinQuantityPD : "${item.cipinQuantityPD}",
				cipinQuantityCY : "${item.cipinQuantityCY}",
				jiupinQuantityWH : "${item.jiupinQuantityWH}",
				jiupinQuantityPD : "${item.jiupinQuantityPD}",
				jiupinQuantityCY : "${item.jiupinQuantityCY}",
				ydengQuantityWH : "${item.ydengQuantityWH}",
				ydengQuantityPD : "${item.ydengQuantityPD}",
				ydengQuantityCY : "${item.ydengQuantityCY}",
				freezeQuantityWH : "${item.freezeQuantityWH}",
				freezeQuantityPD : "${item.freezeQuantityPD}",
				freezeQuantityCY : "${item.freezeQuantityCY}", 
				remark : "${item.remark}"
		    });
		</c:forEach>
	}
	if('${pd.zd003BillStatus}' == '1'){
		$("#save").hide();
		$("input[type!='hidden']").attr("disabled",true);//禁用SELECT标签
		$("select").attr("disabled",true);//禁用SELECT标签
		$("textarea").attr("disabled",true);//禁用TEXTAREA标签
	}
    $("#apply").change(function(){ 
    	$("#oriCode").val($("#apply").find("option:selected").text());
    	<c:forEach items='${missionlist}' var='state'>
    		if($("#apply").find("option:selected").text()=='${state.zd002TackCode}'){
    			$("#whCode").val('${state.zd001WhCode}');
    			$("#whName").val('${state.zd001WhName}'); 
    		}
		</c:forEach>
		var path = basic_Url+"/pditemlist.do?checkmissionid="+$("#apply").val();
		multipleSearch("#table1",true,"",encodeURI(path));
	}); 
    
    /* <c:forEach items='${pditemlist}' var='item'>
	var manager = $("#table1").ligerGetGridManager();
	manager.addRow2({
		id: "${item.id}",
		checkID : "${item.checkID}",
		materialsID : "${item.materialsID}",
		code: "${item.code}",
		name: "${item.name}",
		maType: "${item.maType}",
		spec: "${item.spec}",
		unit: "${item.unit}",
		batch: "${item.batch}",
		quantityWH : "${item.quantityWH}",
		quantityPD : "${item.quantityPD}",
		quantityCY : "${item.quantityCY}",
		cipinQuantityWH : "${item.cipinQuantityWH}",
		cipinQuantityPD : "${item.cipinQuantityPD}",
		cipinQuantityCY : "${item.cipinQuantityCY}",
		jiupinQuantityWH : "${item.jiupinQuantityWH}",
		jiupinQuantityPD : "${item.jiupinQuantityPD}",
		jiupinQuantityCY : "${item.jiupinQuantityCY}",
		ydengQuantityWH : "${item.ydengQuantityWH}",
		ydengQuantityPD : "${item.ydengQuantityPD}",
		ydengQuantityCY : "${item.ydengQuantityCY}",
		freezeQuantityWH : "${item.freezeQuantityWH}",
		freezeQuantityPD : "${item.freezeQuantityPD}",
		freezeQuantityCY : "${item.freezeQuantityCY}", 
		remark : "${item.remark}"
    });
	</c:forEach> */		
	
});
/* 
function Importexcel(){
	var url = basic_WUrl+"/upload.do";
	ShowIframeDialog("operate","上传excel",url,1000,500);	
} */
function submitForm(param){
    //设置ajax全局同步
	var data = grid.getData();
	if(data.length==0){
		$.dialog.alert("请选择物料");
   	 	return false;
	}
	var ba=true;
	if(data !=null){
		var i=0;
		$(data).each(function (){
			i++;
			if(this.ydengQuantityPD<0||this.cipinQuantityPD<0||this.jiupinQuantityPD<0||this.freezeQuantityPD<0){
				$.dialog.alert("您第"+i+"行的盘点数量不能为负数");
				ba=false;
				return false;
			}
	 	});	
	}
	jQuery.ajaxSettings.async = false;
	if(ba){
		$("#checkdata").val(JSON.stringify(data));
	  	$("#submitFlag").val(param);
	  	$("#billStatus").val(param);
	    $("#inputForm").submit();
	}
 }		

function tableSubmit(msg,param){
	$.ligerDialog.confirm(msg, 
			function (flag) { 
			if(flag){
				submitForm(param);
				}
		}); 
}

function submitSave(param){
	tableSubmit('是否确认提交本次盘点？',param);
}
	
</script>

</head>

<body>

<form id="inputForm" action="" method="post">
<input type="hidden" id="id" name="id" value="${pd.id}"/>
<input type="hidden" id="checkdata" name="checkdata" value=""/>
<input type="hidden" id="itemdelete" name="itemdelete" value=""/>
<input type="hidden" id="version" name="bizVersion" value="${pd.bizVersion}"/>
<input type="hidden" id="billStatus" name="zd003BillStatus" value="${pd.zd003BillStatus}"/>
<!-- 用于标识提交按钮 -->
<input type="hidden" id="submitFlag" value="" />
	<div class="pageContent" >
   		<div class="panel">
	    	<table class="TableBlock" align="center">
	    		<c:if test="${!empty pd.zd002TackCode }">
					<tr id="tr1">
						<td nowrap>单据编号:</td>
						<td >
	      					<input type="text" id="checkCode" name="zd002TackCode" readonly="readonly" class="inp_width_98p" value="${pd.zd002TackCode}" />
	       				</td>
	       				<td nowrap="nowrap" width="20%">建单时间:</td>
						<td width="30%"><input class="inp_width_98p" type="text" id="dctime" name="bizCreatorDate"
							value="<fmt:formatDate value="${empty pd.bizCreatorDate?nowDate:pd.bizCreatorDate}" type="both" dateStyle="long" pattern="yyyy-MM-dd" />" readonly="readonly"/></td>
	    			</tr>
	   			</c:if> 
	   			<tr>
	   				<td>任务单号：</td><!-- 不定 -->
	   					<c:choose>
    					<c:when test="${empty pd.zd002TackCode}">
	    					<td>
	    						<select id="apply" name="apply" class="inp_width_98p required" >
	                 				<option></option>
	                 				<c:forEach items='${missionlist}' var='state'>
	                 					<option id="${ state.zd002TackCode}" value='${state.id}'>${state.zd002TackCode}</option>
	                 				</c:forEach>
	                			</select> 
	               				<input type="hidden" id="oriCode" name="zd002TackCode"  value="" />	                	
	                 			<script type="text/javascript">
	               	 			 $("#"+'${pd.oriCode}').attr("selected","selected");              	
	               	 			</script>   
	            			</td>
           	 			</c:when>
            			<c:otherwise>
            				<td> <input type="text" id="oriCode" class="inp_width_98p" name="zd002TackCode" value="${pd.zd002TackCode}" readonly="readonly"/></td>
           				</c:otherwise>
            			</c:choose>  
	   				<td>盘点时间:</td>
	   				<td>
		   				<input id="pdDate" name="bizModifyDate" class="inp_width_98p required" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})"
								value="<fmt:formatDate value="${pd.bizModifyDate}" pattern="yyyy-MM-dd" type="both" />" />
						<c:if test="${pd.bizModifyDate == null}">
							<script>
								var date = new Date();
								$("#pdDate").val(date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate());
							</script>
						</c:if>
					</td>
	   			</tr>
	   			<tr>
	   				<td nowrap>盘点仓库:</td>
	   				<td>
	                 	<input type="text" id="whName" name="zd001WhName" class="inp_width_98p required" readonly="readonly" value="${pd.zd001WhName}"/>
	                 	<input type="hidden" id="whCode" name="zd001WhCode" value="${pd.zd001WhCode}"/>
   		             </td>    	
 		             <td nowrap>盘点人:</td>
 		             <td>
 		             	<input type="text" id="agent" name="zd004Agent" class="inp_width_98p" value="${user.name}" readonly="readonly"/>
						<c:if test="${pd.zd004Agent != null}">
							<script>
								$("#agent").val("${pd.zd004Agent}");
							</script>
						</c:if>
 		             </td>
   		         </tr>
 		         <tr>
 		          	<td nowrap>备注:</td>
 		           	<td colspan="3"><input id="remark" class="inp_width_98p" name="zd005Remark" value="${pd.zd005Remark}" /></td>
 		         </tr>
            </table>
         <div id="table1"> </div>               
         </div>
         <div class="formBar">
	        <ul>
		       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save" onclick="submitSave(1)">保存</button></div></div></li>
		       <li><div class="button"><div class="buttonContent"><button type="button" class="print" onclick="CloseDialog()">关闭</button></div></div></li>
	        </ul>
        </div>    
    </div>
</form>
</body>
</html>
